#pragma once

#include <windows.h> // Include to the core Win32 API
#include <tchar.h>	 // Include UNICODE support
#include <sstream>	 // Include wostringstream support

#if defined(_DEBUG)
#include <iostream>
#endif

#include "resource.h" // Include resource IDs

#include <objbase.h> // Include COM Library support
#include <commdlg.h> // Include the Common Dialogs.

// Direct2D Helper Macros:
#define D2DColor(clr) D2D1::ColorF(D2D1::ColorF::clr)

#include <dwrite.h> // Include the DirectWrite API.
#pragma comment(lib, "dwrite.lib") // Connect to the DirectWrite Import Library.

#include <process.h> // Include Multi-Threading functions:

// Include the Windows Multimedia Library:
#include <mmsystem.h>               // Multimedia header file.
#pragma comment(lib, "winmm.lib")   // Multimedia import library.

#include "PhysicsCore.h"

#include <wincodec.h>
#pragma comment(lib, "windowscodecs.lib")

#include <map>
#include "ImageLoader.h"
#include "Game.h"


#define WM_SFX WM_USER+1

class Game;

using namespace std;

class Core
{
public:
	BOOL bRunning;
	BOOL resize;
	BYTE keys[256];
	D2D1_SIZE_F mouseCoord;
	HANDLE hGameThread; // The GameMain Thread Handle
	
	//static Game * GetInstance() { static Game pong; return &pong; }
	//Game::GetInstance()->
	Core();
	virtual ~Core();

	void Startup();
	void Shutdown();
	void GetInput();
	void Simulate();
	void Render();
	void SetDisplayMode(int iWidth, int iHeight, int iBpp, int iRefreshRate);
	void ToggleFullscreenMode(HWND hWnd, BOOL bFullScreen,int iWidth, int iHeight, int iBpp, int iRefreshRate);

	void resizeObjects();
	void resizeClient(WORD width, WORD height);

	void RawKeyInput(RAWKEYBOARD& kb);
	void RawMouseInput(RAWMOUSE& mouse);
	void SetCursorPos(LONG width, LONG height);

	void SetHwnd(HWND _hWnd) {hWnd = _hWnd;}
	void SetGame(Game* _game){ game = _game; }

	ID2D1Bitmap* LoadImage(PCWSTR name);

private:
	PhysicsCore phyCore;

	// Window Handle:
	HWND hWnd;

	ImageLoader imageLoader;

	// Direct2D Helper Functions:
	HRESULT CreateGraphics(HWND hWnd);
	void DestroyGraphics();

	// Direct2D Global Interfaces:
	ID2D1Factory* pD2DFactory;
	ID2D1HwndRenderTarget* pRT;
	ID2D1SolidColorBrush *pBrush;

	// DirectWrite Global Interfaces:
	IDWriteFactory* pDWFactory;
	IDWriteTextFormat* pTF;

	// WIC Interfaces:
	IWICImagingFactory *pWICFactory;

	DWORD dwFrames;
	DWORD dwCurrentTime;
	DWORD dwLastUpdateTime;
	DWORD dwElapsedTime;
	TCHAR szFPS[32];

	Game* game;

	//Textures Storage
	map<PCWSTR, ID2D1Bitmap *> imagesMap;
};